package org.example.template.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;

public class DateUtil {
	static SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
	/**
	 * 获取当天的开始时间
	 * @return
	 */
	public static Date getDayBegin() {
		Calendar cal = new GregorianCalendar();
		cal.set(Calendar.HOUR_OF_DAY, 0);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.SECOND, 0);
		cal.set(Calendar.MILLISECOND, 0);
		return cal.getTime();
	}

	/**
	 * 获取当天的结束时间
	 * @return
	 */
	public static Date getDayEnd() {
		Calendar cal = new GregorianCalendar();
		cal.set(Calendar.HOUR_OF_DAY, 23);
		cal.set(Calendar.MINUTE, 59);
		cal.set(Calendar.SECOND, 59);
		return cal.getTime();
	}
	//日期格式转化
	public static List<String> getDateList(String param) throws ParseException{
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Date date = sdf.parse(param);
		List<String> dateList = new ArrayList<>();
		List<Date> list = getAllTheDateOftheMonth(date);
		for(Date dates: list){
			dateList.add(sdf.format(dates));
		}
		return dateList;
	}
	//根据当前日期获得本月的所有日期
	private static List<Date> getAllTheDateOftheMonth(Date date) {
		List<Date> list = new ArrayList<Date>();
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.set(Calendar.DATE, 1);
		int month = cal.get(Calendar.MONTH);
		while(cal.get(Calendar.MONTH) == month){
			list.add(cal.getTime());
			cal.add(Calendar.DATE, 1);
		}
		return list;
	}
	/**
	 * 获取日期年份
	 * @param date
	 * @return
	 * @throws ParseException
	 */
	public static int getYear(String date) throws ParseException{
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(dateFormat.parse(date));
		return calendar.get(Calendar.YEAR);
	}

	/**
	 * 获取日期月份
	 * @param date
	 * @return
	 * @throws ParseException
	 */
	public static int getMonth(String date) throws ParseException{
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(dateFormat.parse(date));
		return (calendar.get(Calendar.MONTH) + 1);
	}

	/**
	 * 获取日期号
	 * @param date
	 * @return
	 * @throws ParseException
	 */
	public static int getDay(String date) throws ParseException{
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(dateFormat.parse(date));
		return calendar.get(Calendar.DAY_OF_MONTH);
	}
	/**
	 * 获取月份起始日期
	 * @param date
	 * @return
	 * @throws ParseException
	 */
	public static String getMinMonthDate(String date) throws ParseException{
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(dateFormat.parse(date));
		calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH));
		return dateFormat.format(calendar.getTime());
	}

	/**
	 * 获取月份最后日期
	 * @param date
	 * @return
	 * @throws ParseException
	 */
	public static String getMaxMonthDate(String date) throws ParseException{
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(dateFormat.parse(date));
		calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
		return dateFormat.format(calendar.getTime());
	}

    /***
     *获得当前时间前指定几个小时的时间
     * @param ihour
     * @return String
     * @throws ParseException
     */
    public static String getBeforeHourTime(int ihour){
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, calendar.get(Calendar.HOUR_OF_DAY) - ihour);
        SimpleDateFormat df = new SimpleDateFormat("HH");
        String returnstr = df.format(calendar.getTime());
        return returnstr;
    }

	//返回格式YYYY-MM-DD HH:MM:SS
	public static String getBeforeHour(int ihour){
		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.HOUR_OF_DAY, calendar.get(Calendar.HOUR_OF_DAY) - ihour);
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String returnstr = df.format(calendar.getTime());
		return returnstr;
	}


	//输出指定两个日期之间的所有日期
	public static List<String> getTimeScope(String date1, String date2){
		List<String> timeList = new ArrayList<>();
		if(date1.equals(date2)){
			System.out.println("两个日期相等!");
			return null;
		}

		String tmp;
		if(date1.compareTo(date2) > 0){  //确保 date1的日期不晚于date2
			tmp = date1; date1 = date2; date2 = tmp;
		}

		tmp = dateFormat.format(str2Date(date1).getTime());

		int num = 0;
		while(tmp.compareTo(date2) <= 0){
			timeList.add(tmp);
			num++;
			tmp = dateFormat.format(str2Date(tmp).getTime() + 3600*24*1000);
		}
		return timeList;
	}

	private static Date str2Date(String str) {
		if (str == null) return null;
		try {
			return dateFormat.parse(str);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * @Description: String类型毫秒数转换成日期
	 * [@param](http://my.oschina.net/param) lo 毫秒数
	 * @return String yyyy-MM-dd HH:mm:ss
	 */
	public static String stringToDate(String lo){
		long time = Long.parseLong(lo);
		Date date = new Date(time);
		SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return sd.format(date);
	}

	public static Date stringToDateTime(String lo){
		try {
			SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			return sd.parse(lo);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * @Description: long类型转换成日期
	 *
	 * @param lo 毫秒数
	 * @return String yyyy-MM-dd HH:mm:ss
	 */
	public static String longToDate(long lo){
		Date date = new Date(lo);
		SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return sd.format(date);
	}


	/**
	 * 获取一天前的日期
	 * @param date
	 * @return
	 */
	public static String getBeforeDate(String date) {
		String dateTime = "";
		try {
			SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
			Date date1 = df.parse(date);
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(date1);
			calendar.set(Calendar.DATE,calendar.get(Calendar.DATE)-1);
			dateTime = df.format(calendar.getTime()) +" 23:59:59";
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return dateTime;
	}

	/**
	 * 获取一天小时的日期
	 * @param date
	 * @return
	 */
	public static String getBeforeHour(String date) {
		String beforeHour = "";
		try{
			SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			Date date1 = df.parse(date);
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(date1);
			calendar.set(Calendar.HOUR_OF_DAY, calendar.get(Calendar.HOUR_OF_DAY)-1);
			beforeHour = df.format(calendar.getTime());
		}catch (ParseException e){
			e.printStackTrace();
		}
		return beforeHour;
	}

	/**
	 * 通过时间秒毫秒数判断两个时间的间隔
	 * @param date1
	 * @param date2
	 * @return
	 */
	public static int differentDaysByMillisecond(Date date1, Date date2) {
		int days = (int) ((date2.getTime() - date1.getTime()) / (1000*3600*24));
		return days;
	}

	public static long StringToLong(String time) {
		try {
			SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			return sd.parse(time).getTime();
		} catch (ParseException e) {
			return 0l;
		}
	}

	public LocalDateTime stringSetLocalDateTime(String time){
		DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
		time = time.replaceAll("T", " ");
		LocalDateTime ldt = LocalDateTime.parse(time,df);
		return ldt;
	}

	public static String LocalDateTimeNow(LocalDateTime time) {
		DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
		return df.format(time);
	}


	public static void main(String[] args) {
		try {
			SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			//long diffTime = Long.parseLong("1576517122")*1000l;
			long diffTime = Long.parseLong("1576517122937");
			long startTime = df.parse("1970-01-01 00:00:00").getTime();
			System.out.println(longToDate(diffTime+startTime));
		} catch (Exception e) {
		}
	}
}
